package color.koitq.picker.activty;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;

import com.qmuiteam.qmui.widget.QMUITopBarLayout;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

import color.koitq.picker.R;
import color.koitq.picker.ad.AdActivity;
import color.koitq.picker.entity.ArticleModel;
import color.koitq.picker.view.ProgressWebView;
import butterknife.BindView;

public class ArticleDetailActivity extends AdActivity {

    @BindView(R.id.topbar)
    QMUITopBarLayout topBar;
    @BindView(R.id.webView)
    ProgressWebView webView;
    @BindView(R.id.bannerView)
    ViewGroup bannerView;

    public static void showDetail(Context context, ArticleModel tab1Model) {
        Intent intent = new Intent(context,ArticleDetailActivity.class);
        intent.putExtra("model",tab1Model);
        context.startActivity(intent);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.web_ui;
    }

    @Override
    protected void init() {
        topBar.addLeftImageButton(R.mipmap.back_white_icon,R.id.topbar_right_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        ArticleModel tab1Model = (ArticleModel) getIntent().getSerializableExtra("model");
        topBar.setTitle("详情");
        getArticle(tab1Model.url);
    }

    private void getArticle(String url) {
        showLoading("加载中...");
        new Thread() {
            @Override
            public void run() {
                super.run();
                Document doc = null;
                try {
                    doc = Jsoup.connect(url).get();
                    Elements elements = doc.getElementsByClass("single-content");
                    Elements imgs = doc.getElementsByTag("img");
                    for (Element element : imgs) {
                        element.removeAttr("style");
                        element.attr("width","100%").attr("height","auto");
                    }

                    Elements links = doc.getElementsByTag("a");
                    for (Element element : links) {
                        element.removeAttr("href");
                    }

                    Elements replyread = doc.getElementsByClass("reply-read");
                    if (replyread != null) {
                        replyread.remove();
                    }

                    String result = elements.html();
                    String finalResult = result;
                    topBar.post(new Runnable() {
                        @Override
                        public void run() {
                            hideLoading();
                            webView.loadDataWithBaseURL(null, finalResult,"text/html",  "utf-8",null);
                        }
                    });
                } catch (IOException e) {
                    topBar.post(new Runnable() {
                        @Override
                        public void run() {
                            hideLoading();
                            showErrorTip(topBar,"加载失败，请重试！");
                        }
                    });
                }

            }
        }.start();
    }
}
